home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 001 / pibt40s2.arc / PIBCRC.MOD < prev    next >
Text File  |  1986-09-29  |  6KB  |  90 lines

  1. (*----------------------------------------------------------------------*)
  2. (*             Update_CRC --- Update cyclic redundancy check            *)
  3. (*----------------------------------------------------------------------*)
  4.  
  5. FUNCTION Update_CRC( Old_CRC : INTEGER; C : BYTE ) : INTEGER;
  6.  
  7. (*----------------------------------------------------------------------*)
  8. (*                                                                      *)
  9. (*     Function:   Update_CRC                                           *)
  10. (*                                                                      *)
  11. (*     Purpose:    Computes cyclic redundancy check for 1 character     *)
  12. (*                                                                      *)
  13. (*     Calling Sequence:                                                *)
  14. (*                                                                      *)
  15. (*        New_CRC := Update_CRC( Old_CRC : INTEGER;                     *)
  16. (*                               C       : BYTE ) : INTEGER;            *)
  17. (*                                                                      *)
  18. (*           Old_CRC --- previous CRC value.                            *)
  19. (*           C       --- character for which CRC is computed.           *)
  20. (*           New_CRC --- updated CRC.                                   *)
  21. (*                                                                      *)
  22. (*     Calls:    None                                                   *)
  23. (*                                                                      *)
  24. (*     Remarks:  This uses a tabular method to speed up the CRC         *)
  25. (*               computation.  The table was obtained as the successive *)
  26. (*               CRC values formed by computing:                        *)
  27. (*                                                                      *)
  28. (*                  FOR I := 0 TO 255 DO                                *)
  29. (*                     BEGIN                                            *)
  30. (*                        CRCTab[I] := 0;                               *)
  31. (*                        CRCTab[I] := Update_CRC( CRCTab[I], I );      *)
  32. (*                     END;                                             *)
  33. (*                                                                      *)
  34. (*               where 'Update_CRC' computes a CRC value using one of   *)
  35. (*               non-tabular methods.                                   *)
  36. (*                                                                      *)
  37. (*               For better speed, use the two statements in the body   *)
  38. (*               of this procedure directly where you need to compute   *)
  39. (*               the CRC to avoid subroutine overhead.                  *)
  40. (*                                                                      *)
  41. (*               Incidentally, when using this routine for Xmodem and   *)
  42. (*               friends, you DO NOT pass two extra zero bytes through  *)
  43. (*               to compute the CRC as you do when using the more       *)
  44. (*               brute force approaches.                                *)
  45. (*                                                                      *)
  46. (*----------------------------------------------------------------------*)
  47.  
  48. (* STRUCTURED *) CONST
  49.    CRCTab : ARRAY[BYTE] OF INTEGER =
  50.             (      0,   4129,   8258,  12387,  16516,  20645,  24774,  28903,
  51.               -32504, -28375, -24246, -20117, -15988, -11859,  -7730,  -3601,
  52.                 4657,    528,  12915,   8786,  21173,  17044,  29431,  25302,
  53.               -27847, -31976, -19589, -23718, -11331, -15460,  -3073,  -7202,
  54.                 9314,  13379,   1056,   5121,  25830,  29895,  17572,  21637,
  55.               -23190, -19125, -31448, -27383,  -6674,  -2609, -14932, -10867,
  56.                13907,   9842,   5649,   1584,  30423,  26358,  22165,  18100,
  57.               -18597, -22662, -26855, -30920,  -2081,  -6146, -10339, -14404,
  58.                18628,  22757,  26758,  30887,   2112,   6241,  10242,  14371,
  59.               -13876,  -9747,  -5746,  -1617, -30392, -26263, -22262, -18133,
  60.                23285,  19156,  31415,  27286,   6769,   2640,  14899,  10770,
  61.                -9219, -13348,  -1089,  -5218, -25735, -29864, -17605, -21734,
  62.                27814,  31879,  19684,  23749,  11298,  15363,   3168,   7233,
  63.                -4690,   -625, -12820,  -8755, -21206, -17141, -29336, -25271,
  64.                32407,  28342,  24277,  20212,  15891,  11826,   7761,   3696,
  65.                  -97,  -4162,  -8227, -12292, -16613, -20678, -24743, -28808,
  66.               -28280, -32343, -20022, -24085, -12020, -16083,  -3762,  -7825,
  67.                 4224,    161,  12482,   8419,  20484,  16421,  28742,  24679,
  68.               -31815, -27752, -23557, -19494, -15555, -11492,  -7297,  -3234,
  69.                  689,   4752,   8947,  13010,  16949,  21012,  25207,  29270,
  70.               -18966, -23093, -27224, -31351,  -2706,  -6833, -10964, -15091,
  71.                13538,   9411,   5280,   1153,  29798,  25671,  21540,  17413,
  72.               -22565, -18438, -30823, -26696,  -6305,  -2178, -14563, -10436,
  73.                 9939,  14066,   1681,   5808,  26199,  30326,  17941,  22068,
  74.                -9908, -13971,  -1778,  -5841, -26168, -30231, -18038, -22101,
  75.                22596,  18533,  30726,  26663,   6336,   2273,  14466,  10403,
  76.               -13443,  -9380,  -5313,  -1250, -29703, -25640, -21573, -17510,
  77.                19061,  23124,  27191,  31254,   2801,   6864,  10931,  14994,
  78.                 -722,  -4849,  -8852, -12979, -16982, -21109, -25112, -29239,
  79.                31782,  27655,  23652,  19525,  15522,  11395,   7392,   3265,
  80.                -4321,   -194, -12451,  -8324, -20581, -16454, -28711, -24584,
  81.                28183,  32310,  20053,  24180,  11923,  16050,   3793,   7920 );
  82.  
  83. BEGIN (* Update_CRC *)
  84.  
  85.    Old_CRC    := SWAP( Old_CRC ) XOR C;
  86.    Update_CRC := ( Old_CRC AND $FF00 ) XOR CRCTab[ LO( Old_CRC ) ];
  87.  
  88. END   (* Update_CRC *);
  89.  
  90.